//an actual shader
//TODO: need to add an ability to display something anyway on failure

var doll = (function(_doll) {
    _doll.toon_shader = {
        uniforms: {
	    "uDirLightPos":	{ type: "v3", value: new THREE.Vector3() },
	    "uDirLightColor": { type: "c", value: new THREE.Color( 0xffffff ) },
	    "uMaterialColor":  { type: "c", value: new THREE.Color( 0xffffff ) },
	    uKd: {
	        type: "f",
	        value: 0.7
	    },
	    uBorder: {
	        type: "f",
	        value: 0.4
	    }
        },
        material: null,
        on_load: function() {}
    };

    //call to fetch this shader for your scene
    _doll.toon_shader.fetch = function (use_load_screen) {
        if(_doll.toon_shader.fs && _doll.toon_shader.vs) {
            console.log("toon_shader already loaded..");
            return;//already fetched this shader it is ready to use
        }

        console.log("fetch toon_shader..");
        _doll.toon_shader.fs = "";
        _doll.toon_shader.vs = "";
        _doll.fetch.shader('toon.vs', _doll.toon_shader.vs_fetch, use_load_screen);
        _doll.fetch.shader('toon.fs', _doll.toon_shader.fs_fetch, use_load_screen);
    };

    _doll.toon_shader.vs_fetch = function(shader, sh) {
        _doll.toon_shader.vs = shader;
        _doll.toon_shader.vsh = sh;
        _doll.toon_shader.make_material();
    };

    _doll.toon_shader.fs_fetch = function(shader, sh) {
        _doll.toon_shader.fs = shader;
        _doll.toon_shader.fsh = sh;
        _doll.toon_shader.make_material();
    };

    _doll.toon_shader.make_material = function () {
        if(!_doll.toon_shader.fs || !_doll.toon_shader.vs) {
            return;
        }
        console.log('Making toon_shader.material..');
        var u = THREE.UniformsUtils.clone(_doll.toon_shader.uniforms);
        _doll.toon_shader.material = new THREE.ShaderMaterial(
            {uniforms: u, 
             vertexShader: _doll.toon_shader.vs, 
             fragmentShader: _doll.toon_shader.fs}
        );

        _doll.toon_shader.on_load();
    };

    //gives a copy of the material
    _doll.toon_shader.copy = function() {
        var u = THREE.UniformsUtils.clone(_doll.toon_shader.uniforms);
        var mat = new THREE.ShaderMaterial(
            {uniforms: u, 
             vertexShader: _doll.toon_shader.vs, 
             fragmentShader: _doll.toon_shader.fs}
        );
        return mat;
    };
    return _doll;
}(doll || {}));
